package com.raincat.multimediapicker;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.raincat.multimediapicker.utils.AlbumHelper;
import com.raincat.multimediapicker.utils.AlbumHelper.onImagesBucketListGetListener;
import com.raincat.multimediapicker.utils.ImageBucket;

import java.util.ArrayList;
import java.util.List;

public class ImageFolderFragment extends Fragment {
    private Context context;

    private ListView listView;

    private ProgressDialog dialog;
    private List<ImageBucket> dataList = new ArrayList<>();

    @SuppressWarnings("deprecation")
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        context = this.getActivity();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.picker_fragment_folder, container, false);
        listView = (ListView) view.findViewById(R.id.list);
        setData();
        listener();
        return view;
    }

    private void setData() {
        dialog = ProgressDialog.show(context, null, getString(R.string.picker_now_loading_please_wait));
        AlbumHelper.getInstance(context).getImagesBucketList(new onImagesBucketListGetListener() {
            @Override
            public void onFinish(List<ImageBucket> bucketList) {
                dataList = bucketList;
                ((Activity) context).runOnUiThread(new Runnable() {
                    public void run() {
                        dialog.dismiss();
                        ListAdapter mListAdapter = new ListAdapter(context);
                        listView.setAdapter(mListAdapter);
                    }
                });
            }
        });
    }


    private void listener() {
        listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Bundle bundle = new Bundle();
                bundle.putInt("Index", position);
                ((PickerActivity) context).fragmentTabAdapter.addFragment(new ImageGridFragment(), bundle);
            }
        });
    }

    private class ListAdapter extends BaseAdapter {
        private LayoutInflater mInflater;

        ListAdapter(Context c) {
            mInflater = LayoutInflater.from(c);
        }

        @Override
        public int getCount() {
            return dataList.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Holder holder;
            if (convertView == null) {
                holder = new Holder();
                convertView = mInflater.inflate(R.layout.picker_fragment_folder_item, parent, false);
                holder.image = (ImageView) convertView.findViewById(R.id.iv_img);
                holder.name = (TextView) convertView.findViewById(R.id.tv_name);
                holder.count = (TextView) convertView.findViewById(R.id.tv_count);
                convertView.setTag(holder);
            } else {
                holder = (Holder) convertView.getTag();
            }

            ImageBucket item = dataList.get(position);
            holder.count.setText("(" + item.count + ")");
            holder.name.setText(item.bucketName);
            if (item.imageList != null && item.imageList.size() > 0) {
                final String sourcePath = item.imageList.get(0).imagePath;
                if (!TextUtils.isEmpty(sourcePath)) {
                    Glide.with(context).asBitmap().load(sourcePath).into(holder.image);
                }
            } else {
                holder.image.setImageBitmap(null);
            }
            return convertView;
        }

        class Holder {
            ImageView image;
            TextView name, count;
        }
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

}
